package com.ben.smm.broadcast;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.util.Log;
import android.widget.Toast;

import com.ben.smm.util.SMMConstants;

/**
 * 短信发送成功回执
 * 
 * @author lixuanbin
 */
public class SentReceiver extends BroadcastReceiver {
	private int receiverCount = 0;

	@Override
	public void onReceive(Context context, Intent intent) {
		String action = intent.getAction();
		if (action != null && SMMConstants.SMS_SENT.equals(action)) {
			int resultCode = getResultCode();
			Bundle bundle = intent.getExtras();
			String phone = bundle.getString("phone");
			receiverCount += 1;
			Object pdu = (Object) intent.getExtras().get("pdu");
			if (pdu != null) {
				// Try to get the pdu, but it's always null...
				Log.d("smm", pdu.getClass().toString());
			}
			switch (resultCode) {
			case Activity.RESULT_OK:
				Toast.makeText(context,
				/* "第【" + deliveryCount + "】条" */"发送成功，接收号码【" + phone + "】",
						12000).show();
				// SMMDialogUtil.simpleOkDialog(context, "发送成功", "发送成功，接收号码【"
				// + phone + "】");
				break;
			case SmsManager.RESULT_ERROR_GENERIC_FAILURE:
			case SmsManager.RESULT_ERROR_RADIO_OFF:
			case SmsManager.RESULT_ERROR_NULL_PDU:
			case SmsManager.RESULT_ERROR_NO_SERVICE:
			default:
				Toast.makeText(context, "发送失败，接收号码【" + phone + "】", 12000)
						.show();
				// SMMDialogUtil.simpleOkDialog(context, "发送失败", "发送失败，接收号码【"
				// + phone + "】");
				break;
			}
			System.out.println(receiverCount);
			context.unregisterReceiver(SentReceiver.this);
		}
	}
}